package com.cssl.gulimall.order.web;

import com.cssl.common.exception.NoStockException;
import com.cssl.common.exception.VerifyPriceException;
import com.cssl.common.vo.order.OrderConfirmVO;
import com.cssl.common.vo.order.OrderSubmitVO;
import com.cssl.common.vo.order.SubmitOrderResponseVO;
import com.cssl.gulimall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

@Controller
public class OrderWebController {
    @Autowired
    private OrderService orderService;

    /**
     * 跳转结算页
     * 购物车cart.html点击去结算跳转到confirm.html结算页
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model, HttpServletRequest request){
        //查询结算页VO
        OrderConfirmVO confirmVO = null;
        try {
            confirmVO = orderService.orderConfirmVO();
            model.addAttribute("confirmOrderData",confirmVO);
            //跳转结算页
            return "confirm";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "toTrade";
    }

    /**
     * 创建订单
     * 创建成功，跳转订单支付页
     * 创建失败，跳转结算页
     * 无需提价要购买的商品，提交订单时会实时查询最新的购物车商品选中的数据提交
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVO vo, Model model, RedirectAttributes attributes){
        try {
            SubmitOrderResponseVO orderResponseVO = orderService.submitOrder(vo);
            //创建订单成功，跳转收银台
            model.addAttribute("submitOrderResp",orderResponseVO); //封装VO订单数据，供页面解析【订单号、应付金额】
            return "pay";
        } catch (Exception e) {
            //下单失败回到订单结算页
            if(e instanceof VerifyPriceException){
                String message = ((VerifyPriceException) e).getMessage();
                attributes.addFlashAttribute("msg","下单失败"+message);
            }else if(e instanceof NoStockException){
                String message = ((NoStockException)e).getMessage();
                attributes.addFlashAttribute("msg","下单失败"+message);
            }else {
                System.out.println(e.getMessage());
            }
            return "redirect:http://order.gulimall.com/toTrade";
        }

    }
}
